package cn.good.wkm.frame.controller;

import cn.good.wkm.frame.common.Result;
import cn.good.wkm.frame.domain.po.User;
import cn.good.wkm.frame.domain.res.UserRes;
import cn.good.wkm.frame.domain.vo.UserVO;
import cn.good.wkm.frame.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author wkm
 * @Date 2024/10/17
 **/

@Slf4j
@RestController
public class UserController {
    @Resource
    private IUserService userService;
    @RequestMapping("queryUserList")
    public UserRes ListUser(){
        try{
            log.info("查询所有用户信息");
            List<UserVO> userVOList = userService.queryUserList();
            return UserRes.builder()
                    .result(Result.buildSuccess())
                    .userVOList(userVOList)
                    .build();
        }catch (Exception e){
            log.error("查询所有用户信息失败",e);
            return UserRes.builder()
                    .result(Result.buildFail())
                    .build();
        }
    }
    @RequestMapping ("getUser/{id}")
    public UserRes getUser(@PathVariable("id") int id){
        try {
            UserVO userVO = userService.getUserInfo(id);
            List<UserVO> userVOList = new ArrayList<>();
            userVOList.add(userVO);
            log.info("获取用户ID {}: 信息成功",id);
            log.info("userId :{} userNickname :{} userHead :{}",userVO.getUserId(),userVO.getUserNickname(),userVO.getUserHead());
            return UserRes.builder()
                    .result(Result.buildSuccess())
                    .userVOList(userVOList)
                    .userVO(userVO)
                    .build();
        }catch (Exception e){
            log.error("查询用户信息失败");
            return UserRes.builder()
                    .result(Result.buildFail())
                    .build();
        }
    }

    @RequestMapping("/addUserInfo/*")
    public UserRes addUserInfo(User user){
        try {
            User user1 =  userService.addUserInfo(user);
            log.info("添加用户id为 {} 的用户成功",user1.getUserId());
            List<UserVO> userVOList = new ArrayList<>();
            UserVO userVO = UserVO.builder()
                    .userId(user1.getUserId())
                    .userNickname(user1.getUserNickname())
                    .userHead(user1.getUserHead())
                    .build();
            userVOList.add(userVO);
            return UserRes.builder()
                    .result(Result.buildSuccess())
                    .userVOList(userVOList)
                    .userVO(userVO)
                    .build();
        }catch (Exception e){
            log.error("添加用户信息失败");
            return UserRes.builder()
                    .result(Result.buildFail())
                    .build();
        }
    }

    @RequestMapping("/delete/{id}")
    public UserRes deleteUserById(@PathVariable Integer id){
        try {
            UserVO userInfo = userService.getUserInfo(id);
            userService.deleteUserById(id);
            List<UserVO> userVOList = new ArrayList<>();
            userVOList.add(userInfo);
            log.info("id为: {}的用户", id + " 删除成功");
            return UserRes.builder()
                    .result(Result.buildSuccess())
                    .userVO(userInfo)
                    .userVOList(userVOList)
                    .build();
        }catch (Exception e){
            log.error("id为: {}的用户", id + " 删除失败");
            return UserRes.builder()
                    .result(Result.buildFail())
                    .build();
        }
    }
    @RequestMapping("/update/*")
    public UserRes UpdateUserById(User user){
       try {
           log.info("id为: {}的用户",user.getUserId() + " 信息修改成功");
           userService.updateUserById(user);
           UserVO userVO = UserVO.builder()
                   .userId(user.getUserId())
                   .userHead(user.getUserHead())
                   .userNickname(user.getUserNickname())
                   .build();
           List<UserVO> userVOList = new ArrayList<>();
           userVOList.add(userVO);
           return UserRes.builder()
                   .result(Result.buildSuccess())
                   .userVO(userVO)
                   .userVOList(userVOList)
                   .build();
       }catch (Exception e){
           log.error("id为: {}的用户",user.getUserId() + " 信息修改失败");
           return UserRes.builder(  )
                   .result(Result.buildFail())
                   .build();
       }
    }
}
